﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Tars.Net.Clients;
using Tars.Net.Codecs;
using Tars.Net.Configurations;
using Tars.Net.Hosting;

namespace QQ
{
    public static class QQClientExtensions
    {
        public static IServiceCollection AddQQTcpClient(this IServiceCollection services)
        {
            return services
                .AddTarsCodecs()
                .ReigsterRpcClients()
                //.AddSingleton<Client.LoginSigInfo>()
                .AddSingleton<Client.QQTcpClient>()
                .AddSingleton<IRpcClient>(x => x.GetRequiredService<Client.QQTcpClient>())
                .AddSingleton<Client.QQController>()
                .AddSingleton<Client.ClientCallback>()
                .AddSingleton<Codecs.IncomingDecoder>()
                .AddSingleton<Codecs.SSOFrameDecoder>()
                .AddSingleton<Codecs.OICQDecoder>()
                .AddSingleton<Utils.ECDHHelper>()
                .AddSingleton<Codecs.wtlogin_login_decoder>()
                .AddSingleton<Client.QQDeviceConfig>()
                .AddSingleton<Packet.Request.OICQRequest>()
                .AddSingleton<Packet.Request.SSOFrame>()
                .AddSingleton<Packet.HeadPacket>()
                .AddSingleton<Codecs.ProtobufDecoder>()
                .AddSingleton<Codecs.RequestDecoder>()
                .AddSingleton<Packet.Request.UniRequest>()
                .AddSingleton<Handler.HeartBeatHandler>()
                .ReigsterRpcServices();
            //TODO:USE REFLECTION TO FUCK THE CODE
        }
        public static IServiceCollection AddConfiguration(this IServiceCollection services)
        {
            services.TryAddSingleton(j =>
            {
                var config = new RpcConfiguration();
                j.GetRequiredService<IConfiguration>().Bind("Host", config);
                return config;
            });

            services.TryAddSingleton(j =>
            {
                var config = new Client.LoginSigInfo();
                j.GetRequiredService<IConfiguration>().Bind("LoginSigInfo", config);
                return config;
            });

            return services;
        }
    }
}
